package com.example.swordkotlin

object Test24 {

    @JvmStatic
    fun main(args: Array<String>) {
        val pre = intArrayOf(1, 2, 4, 7, 3, 5, 6, 8)
        val vin = intArrayOf(4, 7, 2, 1, 5, 3, 8, 6)

        for (i in 0..3) {
            println(i)
        }

        for (i in 0 until 3) {
            println("---$i")
        }

        ReverseList(null)
    }

    class ListNode(var `val`: Int, var next: ListNode? = null)


    /**
     * 翻转链表
     */

    fun ReverseList(head: ListNode?): ListNode? {
        // write code here
        //?.调用操作符 如果 ？前对象是null  则该操作将会返回null 不会抛出异常
        if (head?.next == null) {
            return head
        }

        var cur = head
        var pre: ListNode? = null
        var next: ListNode? = null
        while (cur != null) {
            next = cur.next
            cur.next = pre
            pre = cur
            cur = next
        }
        return pre
    }

    fun ReverseListRecursion(head: ListNode?): ListNode? {
        // write code here
        //?.调用操作符 如果 ？前对象是null  则该操作将会返回null 不会抛出异常
        if (head?.next == null) {
            return head
        }
        val newListNode = ReverseListRecursion(head.next)
        head.next?.next = head
        head.next = null
        return newListNode
    }
}